<?php

namespace app\controller\sys;

use app\BaseController;
use app\model\StationOrder;
use think\Response;

class Index extends BaseController
{
    public function orderStatic(): Response
    {
        $param = $this->request->param();
        if ($param['type'] == "month") {
            $date = strtotime(date("Y-m-01"));
            $arr = array_fill(1, (int)date("d"), 0);
        } else {
            $date = strtotime("-1 month", strtotime(date("Y-m-01")));
            $arr = array_fill(1, (int)date("t", $date), 0);
        }
        $key = [];
        $model = $this->app->make(StationOrder::class);
        foreach ($arr as $k => &$v) {
            $d = date("m月d日", strtotime("+" . ($k - 1) . " day", $date));
            $bet = date("Y-m-d", strtotime("+" . ($k - 1) . " day", $date));
            $key[] = $d;
            $v = $model->where("payment_time", 'between', [$bet . " 00:00:00", $bet . " 23:59:59"])->where('state', 1)->sum('money');;
        }
        return $this->success([
            "key" => $key,
            "data" => [
                ['name' => "流水", "data" => array_values($arr)]
            ]
        ]);
    }

    public function orderMonth(): Response
    {
        $arr = [
            1 => ["name" => "已支付", "value" => 0],
            3 => ["name" => "退款申请", "value" => 0],
            5 => ["name" => "已退款", "value" => 0],
            6 => ["name" => "已取消", "value" => 0]
        ];
        $timer = [
            date("Y-m-01 00:00:00"),
            date("Y-m-d 23:59:59")
        ];
        $order = $this->app->make(StationOrder::class)->whereIn("state", array_keys($arr))->where("create_time", "between", $timer)->group('state')->field("sum(money) money,state")->select()->toArray();
        foreach ($order as $item) {
            $arr[$item["state"]]["value"] = $item["money"];
        }
        return $this->success([['name' => '流水', 'data' => array_values($arr)]]);
    }

    public function orderDay(): Response
    {
        $arr = array_fill(0, (int)date("H") + 1, 0);
//        dd($arr);
        $order = $this->app->make(StationOrder::class)->where("state", 1)->where("payment_time", "between", [date("Y-m-d 00:00:00"), date("Y-m-d H:i:s")])->field("money,payment_time")->select()->toArray();
        $key = [];
        foreach ($order as $item) {
            $h = (int)date("H", strtotime($item["payment_time"]));
            $arr[$h] += $item["money"];
        }
        return $this->success([
            "key" => array_map(function ($its) {
                return ($its < 10 ? "0$its" : $its) . ":00";
            }, array_keys($arr)),
            "data" => [
                ['name' => "流水", "data" => array_values($arr)]
            ]
        ]);
    }
}
